package Clothing.serviceImpl.statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import Clothing.bean.request.statistics.StatisticsRequest;
import Clothing.bean.respose.statistics.StatisticsResponse;
import Clothing.bean.respose.statistics.StatisticsResponse.StatisticsList;
import Clothing.dao.ClothingDao;
import Clothing.dao.LeaseDao;
import Clothing.dao.ReturnDao;
import Clothing.entity.ClothingEntity;
import Clothing.entity.LeaseEntity;
import Clothing.entity.ReturnEntity;
import Clothing.service.statistics.StatisticsService;

@Service("statisticsService")
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private ClothingDao clothingDao;
    
    @Autowired
    private LeaseDao leaseDao;
    
    @Autowired
    private ReturnDao returnDao;
    
    public int statistics(StatisticsRequest request, StatisticsResponse response) {
        Map<String, Object> map  = new HashMap<String, Object>();
        List<StatisticsList> statisticsResultList = new ArrayList<StatisticsResponse.StatisticsList>();
        String leaseStartTime = request.getLeaseStartTime();
        String leaseEndTme = request.getLeaseEndTme();
            
        map.put("leaseStartTIme", leaseStartTime);
        map.put("leaseEndTme", leaseEndTme);
        
        List<ClothingEntity> clothingEntitiyList = new ArrayList<ClothingEntity>();
        clothingEntitiyList = clothingDao.getClothingList(map);
        
        if(clothingEntitiyList.size()!=0){
            for (ClothingEntity clothingEntity : clothingEntitiyList) {
                StatisticsList statisticsList = new StatisticsList();
                statisticsList.setClothingType(clothingEntity.getClothingType());
                map.put("clothingId", clothingEntity.getClothingId());
                int returnCount = 0;//归还数量
                List<ReturnEntity> returnEntity = returnDao.selectReturnByClothingId(map);
                if(returnEntity.size() !=0 && returnEntity!=null){
                    for (int i = 0; i < returnEntity.size(); i++) {
                        returnCount += returnEntity.get(i).getReturnCount();
                    }
                }
                statisticsList.setReturnCount(String.valueOf(returnCount));
                
                statisticsResultList.add(statisticsList);
            }
            
            response.setStatisticsList(statisticsResultList);
            return 0;
        }
        return 1;
    }

}
